package JDBCUI;

import java.sql.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


public class revise extends JFrame {
    private static final long serialVersionUID = -1928970409928880648L;

    JLabel jlpno = new JLabel("奖惩    编号：");
    JLabel jlname = new JLabel("奖惩类型(备注)：");
    JLabel jlaward = new JLabel("奖惩    金额：");
    JLabel jlreason = new JLabel("奖惩    缘由：");

    JTextField jtpno = new JTextField("",20);
    JTextField jtname = new JTextField("",20);
    JTextField jtaward = new JTextField("",20);
    JTextField jtreason = new JTextField("",20);

    JButton buttonchange = new JButton("修改");
    JButton buttonreturn = new JButton("返回");


    public revise() {

        browse browse1 = new browse();


        JPanel jppno = new JPanel();
        JPanel jpname = new JPanel();
        JPanel jpaward = new JPanel();
        JPanel jpreason= new JPanel();

        JPanel jpforbutton = new JPanel(new GridLayout(1,1));

        jppno.add(jlpno);
        jppno.add(jtpno);

        jpname.add(jlname);
        jpname.add(jtname);

        jpaward.add(jlaward);
        jpaward.add(jtaward);

        jpreason.add(jlreason);
        jpreason.add(jtreason);


        jpforbutton.add(buttonchange);
        jpforbutton.add(buttonreturn);

        buttonchange.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
                //关闭修改之前的浏览界面
                browse1.dispose();

                boolean flag =false;
                flag= !jtname.getText().equals("") && !jtreason.getText().equals("");

                System.out.println(flag);


                if ( !flag) {

                    if(e.getSource()==buttonchange){
                        javax.swing.JOptionPane.showMessageDialog(null, "修改格式有误！！！重新修改！");
                    }

                }else {


                    String xname = jtname.getText();
                    String xpno = jtpno.getText();
                    String xaward = jtaward.getText();
                    String xreason = jtreason.getText();


                    Connection conn = null;
                    ResultSet res = null;
                    Statement stat = null;

                    String sql = "SELECT pno,name,award,reason FROM staff;";
                    try{
                        Class.forName("com.mysql.cj.jdbc.Driver");


                    }catch(Exception d){

                        System.out.println("jdbc fall");

                    }
                    try{
                        conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/work","root","123456");
                        stat=conn.createStatement();
                        res=stat.executeQuery(sql);

                        while (res.next())
                        {
                            String i = res.getString(1).trim();
                            if (i.equals(jtpno.getText()))
                            {
                                String sql2="UPDATE staff SET name='"+xname+"'  WHERE pno='"+jtpno.getText()+"'";
                                String sql3="UPDATE staff SET award='"+xaward+"'  WHERE pno='"+jtpno.getText()+"'";
                                String sql4="UPDATE staff SET reason='"+xreason+"'  WHERE pno='"+jtpno.getText()+"'";


                                stat=conn.createStatement();

                                stat.executeUpdate(sql3);
                                stat.executeUpdate(sql4);
                                stat.executeUpdate(sql2);

                                if(e.getSource()==buttonchange){
                                    javax.swing.JOptionPane.showMessageDialog(null, "修改成功!");
                                }
                                try{
                                    stat.close();
                                    conn.close();
                                }catch(SQLException ar){
                                    ar.printStackTrace();
                                }
                                break;
                            }
                        }
                    }catch (SQLException e1) {
                        System.out.println("修改错误");
                        if(e.getSource()==buttonchange){
                            javax.swing.JOptionPane.showMessageDialog(null, "修改格式错误，重新修改!");
                        }
                    }
                    finally{


                        //显示改过的浏览界面
                        browse browse = new browse();

                        System.out.println("修改操作结束");
                        try{
                            conn.close();
                        }catch(SQLException ar){
                            ar.printStackTrace();
                        }
                    }


                }


            }
        });

        buttonreturn.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e){
                //关闭修改界面
                dispose();

            }
        });
        this.setTitle("修改教师奖惩信息");
        this.setLayout(new GridLayout(9,1));
        this.add(jppno);
        this.add(jpname);
        this.add(jpaward);
        this.add(jpreason);


        this.add(jpforbutton);
        this.setLocation(800,300);
        this.setSize(500,400);
        this.setVisible(true);
    }
}
